﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using NHibernate;
using SimpleScrum.Areas.Scrum.Models;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Controllers
{
    public class SprintController : Controller
    {
        public ProjectModel ObjProject;
        public SprintModel ObjSprint;
        private ISession _session = NHibernateHelper.GetSession();

        public SprintController()
        {
            ObjSprint = new SprintModel();
            ObjProject = new ProjectModel();
        }

        // 
        // GET: /Sprint/ 
        [HttpGet]
        public ActionResult Index(Guid projectId)
        {
            IList<Sprint> sprints = ObjSprint.GetSprints(projectId);
            return View(sprints);
        }

        // 
        // GET: /Sprint/Details/
        [HttpGet]
        public ActionResult Details(Guid id)
        {
            Sprint sprint = ObjSprint.GetSprintById(id);
            return View(sprint);
        }

        // 
        // GET: /Sprint/Create 
        [HttpGet]
        public ActionResult New()
        {
            var sprint = new Sprint();

            return View(sprint);
        }

        // 
        // POST: /Sprint/Create 
        [HttpPost]
        public ActionResult New(Sprint sprint)
        {
            try
            {
                sprint.Project = ObjProject.GetProjectById(Client.Instance.CurrentProjectId);
                ObjSprint.NewSprint(sprint);
                return RedirectToAction("Index", "Sprint", new {projectId = Client.Instance.CurrentProjectId});
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /Sprint/Edit/5 
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            Sprint sprint = ObjSprint.GetSprintById(id);
            return View(sprint);
        }

        // 
        // POST: /Sprint/Edit/5 
        [HttpPost]
        public ActionResult Edit(Guid id, Sprint sprint)
        {
            try
            {
                ObjSprint.UpdateSprint(sprint);
                return RedirectToAction("Index", "Sprint", new {projectId = Client.Instance.CurrentProjectId});
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /Sprint/Delete/5 
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            Sprint sprint = ObjSprint.GetSprintById(id);
            return View(sprint);
        }

        // 
        // POST: /Sprint/Delete/5 
        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                Sprint sprint = ObjSprint.GetSprintById(id);
                ObjSprint.DeleteSprint(sprint);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
    }
}